<?php
/**
* @file jiemengtagarticlecontroller.php
* @date: 2016-4-18 下午4:01:08
* @author: xiweijie
*/
namespace haibao\cms\business\control\pc\channel\jiemeng;
use haibao\cms\business\control\ArticleController;
use haibao\cms\web\view\app\article\Preview;

class JieMengTagArticleController extends \haibao\cms\business\control\JiemengController{
	
	public function __construct($updateTime = null,$controlId,$isPublish){
		parent::__construct($updateTime,$controlId,$isPublish);
	}
	
	/**
	 * 显示模版内容（模版动态数据的绑定和模版输出）
	 * @param : String $content
	 * @param : Array $controlData;
	 */
	public function displayTemplateHtml(){
		$controlData = $this->getParameter(self::$CONTROL_DATA);
		$content = $this->getParameter(self::$CONTROL_CONTENT);
		$mergeData = array();
		$moreUrl = $moreName = '';
		if(isset($controlData['manual'])){
		    if(isset($controlData['manual']['type']) && $controlData['manual']['type'] == 'rightCommon'){
		         if($controlData['manual']['isArticle'] == 1){
		             list($mergeData,$moreUrl,$moreName) = $this->getRightCommonData($controlData['manual']);
		             $this->assign('mergeData', $mergeData);
		             $this->assign('moreUrl', $moreUrl);
		             $this->assign('moreName', $moreName);
		         }
		    }elseif(isset($controlData['manual']['type']) && $controlData['manual']['type'] == 'jieMengCategory'){
		    	$category = \haibao\cms\business\Jiemengarticle::getCategory();
		    	ksort($category);
		    	$arrKeyWords = \haibao\cms\business\Jiemengarticle::getAllTitle();
		    	ksort($arrKeyWords);
		    	foreach ($arrKeyWords as $key=>$list){
		    		foreach ($list as $k=>$title){
		    			$textTitle = $title;
		    			$arrKeyWords[$key][$k] = array();
		    			$arrKeyWords[$key][$k]['enCode'] = rawurlencode($textTitle);
		    			$arrKeyWords[$key][$k]['title'] = $textTitle;
		    		}
		    	}
		    	$arrTitle = $arrKeyWords;
				$this->assign('category', $category);
				$this->assign('defaultTitle', array_shift($arrTitle));	 
				$this->assign('arrKeyWords', $arrKeyWords);
		    }else{
		        $this->getActualData($controlData['manual']);
		    }
		}
		$this->assign('wwwHost', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HAIBAO_URL));
		$this->assign('lifeHost', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_LIFE));
		$path = $this->generateHtmlPath(__CLASS__);
		$isNewHtml = $this->generateHtml($path,$content);
		$content = $this->getHtml($path);
		$content = $this->replaceCDNLink($content);
		return $content;
	}
	
	public function getActualData($dataCondition){
		$dataCondition['source'] = __CLASS__;
		$dataCondition['isTag'] = true;
		$dataCondition['isConditionTag'] = true;
		$dataCondition['isPrimaryTag'] = true;
		$dataCondition['isImageColl'] = false;
		$dataCondition['isProImage'] = false;
		$dataCondition['isParentTag'] = false;
		$dataCondition['level'] = 1;
		$this->parseTagId($dataCondition['tagId']);
		$articleList = array();
		if(isset($dataCondition['isReturnTagInfo']) && $dataCondition['isReturnTagInfo']){
			$dataCondition['isReturnTag'] = true;
		}else{
			$dataCondition['isReturnTag'] = false;
		}
		list($articles,$imageColls,$tagInfo) = $this->getArticleControl(1,$dataCondition);
		foreach($articles as $data){
			$temp = array();
			$temp['PublishTime'] = $this->getformatDate($data->PublishTime->format('Y-m-d'));
			$temp['Channel'] = self::getArticleUrl($data->PublishId,520);
			$temp['Id'] = $data->Id;
			$temp['Title'] = $data->Title;
			$temp['ShortTitle'] = $data->ShortTitle?$data->ShortTitle:$data->Title;
			$temp['SiteType'] = $data->SiteType;
			if($tagInfo){
				$temp['TagName'] = $tagInfo[$data->ArticleTag->TagId]['TagName'];
				$temp['TagUrl'] = $tagInfo[$data->ArticleTag->TagId]['Url'];
			}
			array_push($articleList,$temp);
		}
		if(is_array($this->conditionTagId)){
			$topId = $this->conditionTagId[0];
		}else{
			$topId = $this->conditionTagId;
		}
		$hostDomain = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_TOP_DOMAIN);
		$topTagUrl = 'http://life.'.$hostDomain.'/diy/'.$topId.'/';
		$this->assign('topTag', $topId);
		$this->assign('topTagUrl', $topTagUrl);
		$this->assign('articleList',$articleList);
	}
	
	private function getformatDate($date) {
		$finalDate = '';
		$dateTime = strtotime ( $date );
		$today = strtotime(date('Y-m-d'));
		$nowMonth = date ( 'Ym' );
		$month = date ( 'Ym', $dateTime );
		$diff = $today - $dateTime;
		if ($diff == 0 && $month == $nowMonth) {
			$finalDate = '今天';
		} elseif ($diff == 3600*24 && $month == $nowMonth) {
			$finalDate = '昨天';
		} else {
			$finalDate = date ( 'Y-m-d', $dateTime );
		}
		return $finalDate;
	}
	
}